import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useCompanyStore } from './company'
import { getPricesApi } from '@/api/price'
export const usePriceRuleStore = defineStore('priceRule', () => {
    const priceRuleList = ref([])
    const waterPriceRuleList = computed(() => {
        return priceRuleList.value.filter(item => item.type == 1)
    })
    const electricityPriceRuleList = computed(() => {
        return priceRuleList.value.filter(item => item.type == 2)
    })

    const gasPriceRuleList = computed(() => {
        return priceRuleList.value.filter(item => item.type == 3)
    })
    const getPriceRuleNameById = (id) => {
        let priceRule = priceRuleList.value.find(item => item.id == id)
        return priceRule?.name || '-'
    }

    const getPriceRulePriceById = (id) => {
        let priceRule = priceRuleList.value.find(item => item.id == id)
        return priceRule?.fixed_price || 0
    }

    async function fetchPriceRuleList() {
        let { data } = await getPricesApi({
            company_id: useCompanyStore().currentCompany.id
        })
        priceRuleList.value = data
    }

    return {
        priceRuleList,
        waterPriceRuleList,
        electricityPriceRuleList,
        gasPriceRuleList,
        getPriceRuleNameById,
        fetchPriceRuleList,
        getPriceRulePriceById
    }
})